<template>
    <div>
        <rui-page :defines="defines" v-show="!staySettleShow">
            <rui-query :label-width="150"
                       ref="searchForm"
                       :query="queryData"
                       :query-rows="searchFormOptions"
            />
            <rui-table
                :defineId="'defineStaySettle'"
                :fixedLeft="['applyNo']"
                :showIndex="false"
                :columns="staySettleColumns"
                :slots="[{key:'applyNo',slot:'applyNo'}]"
                :select="false"
                @loadDatas="queryData"
                ref-query="searchForm"
                ref="planTable"
            >
                <template slot="toolBar">
                    <Button @click="add" icon="md-add" type="primary">新增</Button>
                </template>
                <template slot-scope="{ row, index }" slot="applyNo">
                    <a @click.prevent="aClick(row)">
                        {{row.applyNo}}
                    </a>
                </template>
            </rui-table>
        </rui-page>

        <ActivatedCase
            :getDataFn="getDataFn"
            :isSP="isSP"
            :modal="modal"
            modalTitle="已激活案件列表"
            :dataDict="dataDict"
            @showContractDetial="showContractDetial"
            @closeModal="closeModal"
            :contractBusiness="contractBusiness"
        />

        <trailSettle
            v-if="staySettleShow"
            :op="op"
            :isSP="isSP"
            :isTrail="isTrail"
            :applyNo = "applyNo"
            :quotationId = "quotationId"
            :contractNo="contractNo"
            @back="close"
        />
    </div>
</template>

<script>

    import ActivatedCase from "_p/basic/pages/contract/common/activated-case/index";
    import {findActivatedContractList} from "_p/basic/api/contract/post-loan-info-change";
    import { channelType } from "_p/basic/api/contract/channel-type/channel-type";
    import { mapGetters } from "vuex";


    import staySettleDefine from "./define/trail-settle-define";
    import {
        getStaySettleList,
        getManagePrepaymentApply,
    } from "@/projects/basic/api/contract/clearance/stay-settle";

    //试算信息
    import trailSettle from "_p/basic/pages/contract/clearance/trail-settle.vue";
    //申请信息
    import applySettle from "_p/basic/pages/contract/clearance/apply-settle.vue";

    export default {
        components: {
            trailSettle,
            applySettle,
            ActivatedCase
        },
        name: "staySettle",
        data() {
            return {
                staySettleColumns:[
                    'applyNo','contractNo','custName','startDate','endDate','loanTerm','alreadyPeriods','noAlreadyPeriods'
                ],
                searchFormOptions: [
                    {defineId: "defineStaySettle", span: 6, fields: ["applyNo",'contractNo','custName']},
                ],

                //试算页面
                staySettleShow:false,
                op:'view',
                isTrail:true,
                applyNo:null,
                quotationId:null,
                contractNo:null,

                //弹窗页面
                modal:false,
                isSP:false,
                dataDict: {}, // 数据字典
                contractBusiness:["assetsChange"],
            }
        },
        methods: {
            refresh() {
                this.$refs['planTable'].reloadData();
            },
            queryData(queryData) {
                getStaySettleList(queryData).then(res => {
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.planTable.updateTableData(records, total);
                    }
                });
            },
            //新增
            add(){
                this.modal = true;
            },
            //查看
            aClick(row){
                this.op = "view";
                this.applyNo = row.applyNo;
                this.quotationId = row.id;
                this.contractNo = row.contractNo;
                this.staySettleShow = true;
            },
            //试算页面关闭
            close(){
                this.staySettleShow = false;
                this.refresh();
            },
            //弹窗回调方法
            showContractDetial(params) {
                if (!params && !params.contractNo) return;
                this.modal = false;

                //打开试算页面
                this.op = "add";
                this.applyNo = params.applyNo;
                this.contractNo = params.contractNo;
                this.quotationId = null;
                this.staySettleShow = true;
            },
            //关闭弹窗
            closeModal() {
                this.modal = false;
            },
        },
        mounted() {
            this.refresh();
        },
        created() {
            this.isSP = channelType(this.$route.meta.params);
        },
        computed: {
            defines() {
                return [
                    {
                        id: "defineStaySettle",
                        fields: staySettleDefine
                    }
                ];
            },
            ...mapGetters({
                userInfo: "userInfo",
                roleType: "roles"
            }),
            getDataFn: () => findActivatedContractList
        },
    }
</script>

<style scoped>

</style>
